package com.fancy.common.dto.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

@Data
@ApiModel("API响应")
public class Result<T> {
    @ApiModelProperty(value = "状态码", example = "200")
    private Integer code;
    
    @ApiModelProperty(value = "响应消息", example = "success")
    private String message;
    
    @ApiModelProperty("响应数据")
    private T data;
    
    @ApiModelProperty(value = "时间戳", example = "1672531200000")
    private Date timestamp;
    
    @ApiModelProperty(value = "请求ID", example = "req_123456")
    private String requestId;
    
    public static <T> Result<T> success(T data) {
        return build(200, "success", data);
    }
    
    public static <T> Result<T> fail(String message) {
        return build(500, message, null);
    }

    public static <T> Result<T> fail(int code, String message) {
        return build(code, message, null);
    }

    private static <T> Result<T> build(Integer code, String message, T data) {
        Result<T> response = new Result<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        response.setTimestamp(new Date());
        response.setRequestId("req_mock_" + System.currentTimeMillis());
        return response;
    }

    public boolean isSuccess() {
        return code != null && code == 200;
    }
}